<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\myshuju\MyHandle;
use app\admin\model\Phpspreadsheet;
use app\BaseController;
use Matrix\Operators\Subtraction;
use think\facade\Db;
use think\Request;

class Lianbiao extends BaseController
{
    public function show_list(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id',null);
        $field = $request->param('field',null);
        $obj = MyHandle::getTable();
        $obj->setModelId($model_id);
//        $obj->setId($id);
        $newstr ='?model_id='.$model_id;
        if($field){
            $newstr .='&pidfield='.$field;
        }
        if($id){
            $newstr .='&pidfieldid='.$id;
        }
        $obj->setPage($request->controller().'/page'.$newstr);
        return $obj->fetch();
    }
    public function page(Request $request){
        $data = MyHandle::GetPage()->getModelPage($request);
        return json_page_response($data);
    }
    public function show_add(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id',null);
        $obj = MyHandle::getForm();
        $obj->setModelId($model_id);
        $obj->setPid($id);
        return $obj->addFormAction(\request()->controller().'/add')
            ->addFormButton([
                [
                    'type'=>'submit',
                    'desc'=>'提交'
                ],
                [
                    'type'=>'back',
                    'desc'=>'返回'
                ]
            ])
            ->fetch();
    }
    public function change(Request $request){
        $model_id = $request->param('model_id');
        $data = $request->param();
        $id = $request->param('id');
        $obj =  MyHandle::GetConfig();
        $obj->setModelId($model_id);
        $obj->setId($id);
        $datas = Db::name($obj->table)
            ->where('id',$data['id'])
            ->find();
        if($datas[$data['field']]==1){
            $datas[$data['field']]=0;
        }else{
            $datas[$data['field']]=1;
        }
        if(Db::name($obj->table)->where('id',$data['id'])->update($datas)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function add(Request $request){
        $obj = MyHandle::GetConfig();
        $data = $obj->getFormData($request);
        if(Db::name($obj->table)->insert($data,true)){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function getchild(Request $request){
        $id = $request->param('id',0);
        $table = $request->param('table');
        $pidfield = $request->param('pidfield');
        $data = Db::name($table)
            ->where('status',1)
            ->where('delete_time',0)
            ->where($pidfield,$id)
            ->select();
        return json_to_respos('获取成功',200,$data);
    }
    public function show_edit(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id',null);
        $obj = MyHandle::getForm();
        $obj->setModelId($model_id);
        $obj->setId($id);
        return $obj->addFormAction(\request()->controller().'/edit')
            ->addFormButton([
                [
                    'type'=>'submit',
                    'desc'=>'提交'
                ],
                [
                    'type'=>'back',
                    'desc'=>'返回'
                ]
            ])
            ->fetch();
    }
    public function edit(Request $request){
        $obj = MyHandle::GetConfig();
        $data = $obj->getFormData($request);
        $data['update_time'] = time();
        if(Db::name($obj->table)->where('id',$data['id'])->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function detail(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id');
        $obj = MyHandle::getForm();
        $obj->setModelId($model_id);
        $obj->setId($id);
        return $obj->addFormAction(\request()->controller().'/edit')
            ->addFormButton([
                [
                    'type'=>'back',
                    'desc'=>'返回'
                ]
            ])
            ->fetch();
    }
    public function del(Request $request){
        $data = $request->param();
        $obj = MyHandle::GetConfig();
        $obj->setModelId($data['model_id']);
        $obj->setId($data['id']);
        $newdata = $obj->getData(true);
        foreach ($newdata as $value){
            if($value['delete']!==1){
                return json_to_respos('有不可删除字段',205);
            }
        }
        if(Db::name($obj->table)->where('id','in',$data['id'])->update(['delete_time'=>time()])){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
    public function exports(Request $request){
        $model_id = $request->param('model_id');
        $obj = MyHandle::GetConfig();
        $obj->setModelId($model_id);
        $where = $obj->getWhere($request);
        $datas = $obj->getDataAll($where);
        $field = $obj->field;
        $excl = new Phpspreadsheet();
        $excl->setColumns('id','序号','text');
        foreach ($field as $value){
            if($value['export']){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='单选框'||$value['form_type']=='多选框'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'||$value['form_type']=='联动单选框'){
                    $excl->setColumns($value['field'],$value['field_name'],'text');
                }elseif ($value['form_type']=='时间'){
                    $excl->setColumns($value['field'],$value['field_name'],'time');
                }elseif ($value['form_type']=='开关'){
                    $arr = explode(',',$value['option']);
                    if(!empty($arr)){
                        $arr = ['开启','关闭'];
                    }
                    $excl->setColumns($value['field'],$value['field_name'],'switch',$arr);
                }
            }
        }
        foreach ($datas as $key=>$value){
            foreach ($field as $k=>$va){
                if($va['form_type']=='联动下拉框'||$va['form_type']=='联动单选框'){
                    if(!$value[$va['field']]){
                        $value[$va['field']] = $va['defaults'];
                        continue;
                    }
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id',$value[$va['field']])
                        ->find();
                    $value[$va['field']] = $shu['name'];
                }
                if($va['form_type']=='开关'){
                    $array = [];
                    if($va['option']){
                        $array = explode(',',$va['option']);
                    }

                    if(empty($array)){
                        $array = ['开启','关闭'];
                    }
                    if($value[$va['field']]==1){
                        $str = $array[0];
                    }else{
                        $str = $array[1];
                    }
                    $value[$va['field']] = $str;
                }
                if($va['form_type']=='联动多选框'){
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id','in',$value[$va['field']])
                        ->select();
                    $str = '';
                    foreach ($shu as $vs){
                        $str.=','.$vs['name'];
                    }
                    $str = trim($str,',');
                    $value[$va['field']] = $str;
                }
                if($va['form_type']=='联动下拉组'){
                    $table1 = explode(',',$va['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$table1[1])->find();
                    $mydata = Db::name($table1['table'])->where('id',$value[$va['field']])->where('status',1)->field('name')->find();
                    if($mydata){
                        $value[$va['field']] = $mydata['name'];
                    }else{
                        $value[$va['field']] = '';
                    }
                }
                if($va['form_type']=='json数组'){
                    $newdatas = json_decode($value[$va['field']],true);
                    $str = '';
                    foreach ($newdatas as $ks=>$vs){
                        $str = $str.$ks.'=>'.$vs.'|';
                    }
                    $str = trim($str,'|');
                    $value[$va['field']] = $str;
                }
            }
            $datas[$key] = $value;
        }
        $excl->setData($datas);
    }

}